package com.tronsis.distribution.activity.material;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageAndVideoPreviewSeeActivity;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.tronsis.distribution.Constants;
import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.ResourceBean;
import com.tronsis.distribution.bean.UploadFileBean;
import com.tronsis.distribution.biz.factory.FileBizFactory;
import com.tronsis.distribution.biz.factory.ResourceBizFactory;
import com.tronsis.distribution.eventbus.EventMessage;
import com.tronsis.distribution.eventbus.EventTag;
import com.tronsis.distribution.http.Url;
import com.tronsis.distribution.util.GlideImageLoader;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.imageloader.ImageLoader;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.util.LogUtil;
import com.tronsis.library.util.SharedPreferencesUtil;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.util.ToastUtil;
import com.tronsis.library.util.WindowUtil;
import com.tronsis.library.widget.ConfirmDialog;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;

import static com.lzy.imagepicker.ImagePicker.REQUEST_CODE_PREVIEW;

public class UploadMaterialActivity extends ToolBarBaseActivity {
    @BindView(R.id.tv_type)
    TextView mTvType;
    @BindView(R.id.ll_type)
    LinearLayout mLlType;
    @BindView(R.id.et_description)
    EditText mEtDescription;
    @BindView(R.id.iv_video)
    ImageView mIvVideo;
    @BindView(R.id.iv_video_delete)
    ImageView mIvVideoDelete;
    @BindView(R.id.iv_play)
    ImageView mIvPlay;
    @BindView(R.id.rv_user_upload)
    RecyclerView mRvUserUpload;

    private static final int REQUEST_CAMERA = 125;
    private static final int REQUEST_ALBUM = 126;
    private static final int REQUEST_VIDEO = 127;

    private PopupWindow mTypePopupWindow;
    private PopupWindow mPopupWindow;


    private CommonAdapter<ImageItem> mCustomImageAdapter;

    private boolean mShowDelete = false;
    private ArrayList<ImageItem> mImageList = new ArrayList<>();
    private ImageItem mImageItem = new ImageItem();
    private String mVideoPath;
    private String mTypeId;
    private ResourceBean mResourceBean;
    LinkedTreeMap<String, String> mTypeLinkedTreeMap = new LinkedTreeMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_material);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.upload_material));
        setRightTvText(getString(R.string.confirm));

        String typeIdsCache = SharedPreferencesUtil.getString(mContext, Constants.SP_MY_RES_TYPE_ID_CACHE);
        if (typeIdsCache != null) {
            mTypeLinkedTreeMap = (LinkedTreeMap<String, String>) new Gson().fromJson(typeIdsCache, new TypeToken<LinkedTreeMap<String, String>>() {
            }.getType());
        }

        mResourceBean = (ResourceBean) getIntent().getSerializableExtra("ResourceBean");
        if (mResourceBean != null) {
            mTypeId = mResourceBean.getTypeId();
            mTvType.setText(mTypeLinkedTreeMap.get(mTypeId));
            mEtDescription.setText(mResourceBean.getContent());
            mVideoPath = mResourceBean.getVideoFid();
            if (!TextUtils.isEmpty(mResourceBean.getImageFid())) {
                String[] strings = mResourceBean.getImageFid().split(",");
                for (String string : strings) {
                    mImageList.add(new ImageItem(string, System.currentTimeMillis()));
                }
            }
            if (!TextUtils.isEmpty(mResourceBean.getVideoFid())) {
                mIvVideo.setVisibility(View.VISIBLE);
                mIvVideo.setImageResource(android.R.color.black);
                mIvPlay.setVisibility(View.VISIBLE);
            }

        }

        initImageRv();

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS && data != null) {
            List<ImageItem> imageItems = (List<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
            if (imageItems != null && imageItems.size() > 0) {
                mImageList.remove(mImageItem);
                mImageList.addAll(imageItems);
                if (mImageList.size() < 9) {
                    mImageList.add(mImageItem);
                }
                mCustomImageAdapter.notifyDataSetChanged();
            }
        }

        if (requestCode == REQUEST_VIDEO && resultCode == RESULT_OK && null != data) {
//            Uri selectedVideo = data.getData();
//            String[] filePathColumn = {MediaStore.Video.Media.DATA};
//
//            Cursor cursor = getContentResolver().query(selectedVideo, filePathColumn, null, null, null);
//            cursor.moveToFirst();
//
//            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
//            String videoPath = cursor.getString(columnIndex);
//            cursor.close();


            Uri selectedImageUri = data.getData();


            mVideoPath = getPath(selectedImageUri);
            LogUtil.e("video", mVideoPath);
            if (TextUtils.isEmpty(mVideoPath)) {
                ToastUtil.showMessage(mContext, "选择视频出错");
                return;
            }
            mIvPlay.setVisibility(View.VISIBLE);
            ImageLoader.display(mContext, new File(mVideoPath), mIvVideo);


        }

        if (requestCode == REQUEST_CODE_PREVIEW) {
            if (mImageList.size() < 3) {
                mImageList.add(mImageItem);
            }
            mCustomImageAdapter.notifyDataSetChanged();
        }

    }

    @Override
    protected void onToolbarClicked(int paramId) {
        super.onToolbarClicked(paramId);
        if (paramId == RIGHT_TEXT) {
            if (TextUtils.isEmpty(mTypeId)) {
                ToastUtil.showMessage(mContext, "请选择类型");
                return;
            }

            String description = mEtDescription.getText().toString();
            if (TextUtils.isEmpty(description)) {
                ToastUtil.showMessage(mContext, "请输入描述");
                return;
            }

            if (TextUtils.isEmpty(mVideoPath) && mImageList.size() == 1) {
                ToastUtil.showMessage(mContext, "请选择图片或者视频");
                return;
            }

            uploadMaterial(mTypeId, description, mImageList, mVideoPath);

        }
    }

    private void uploadMaterial(final String typeId, final String description, final ArrayList<ImageItem> customImageList, String videoPath) {

        final ArrayList<File> fileList = new ArrayList<>();
        final ArrayList<String> idList = new ArrayList<>();

        for (ImageItem imageItem : customImageList) {
            if (!TextUtils.isEmpty(imageItem.path)) {
                fileList.add(new File(imageItem.path));
            }
            if (!TextUtils.isEmpty(imageItem.url)) {
                idList.add(imageItem.url);
            }
        }
       /* if (!TextUtils.isEmpty(mVideoPath)) {
            fileList.add(new File(videoPath));
        }*/

        if (customImageList.size() == 1 && TextUtils.isEmpty(videoPath)) {
            httpUploadResource(description, null, null, typeId);
            return;
        }

        if (!fileList.isEmpty() || !idList.isEmpty()) {
            if (fileList.isEmpty()) {
                httpUploadResource(description, StringUtil.arrayList(idList), null, typeId);
                return;
            }

            try {
                FileBizFactory.createBiz().uploads(this, fileList, Url.FILE_TYPE_IMAGE, new UICallBack() {
                    @Override
                    public void onStart() {
                        showLoadingDialog();
                    }

                    @Override
                    public void onRefresh(Object data) {
                        dismissLoadingDialog();
                        List<UploadFileBean> fileBeanList = (List<UploadFileBean>) data;
                        List<String> fileIds = new ArrayList<>();
                        for (ImageItem imageItem : customImageList) {
                            if (!TextUtils.isEmpty(imageItem.url)) {
                                fileIds.add(imageItem.url);
                            }
                        }
                        for (UploadFileBean uploadFileBean : fileBeanList) {
                            fileIds.add(uploadFileBean.getId());
                        }

                        String ids = StringUtil.arrayList(fileIds);
                        httpUploadResource(description, ids, null, typeId);

                    }

                    @Override
                    public void onFailure(int status) {
                        dismissLoadingDialog();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                dismissLoadingDialog();
            }
        } else {
            File file = new File(videoPath);
            if (!file.exists()) {
                httpUploadResource(description, null, videoPath, typeId);
                return;
            }
            try {
                FileBizFactory.createBiz().upload(this, file, Url.FILE_TYPE_VIDEO, new UICallBack() {
                    @Override
                    public void onStart() {
                        showLoadingDialog();
                    }

                    @Override
                    public void onRefresh(Object data) {
                        UploadFileBean fileBean = (UploadFileBean) data;
                        httpUploadResource(description, null, fileBean.getId(), typeId);
                    }

                    @Override
                    public void onFailure(int status) {
                        dismissLoadingDialog();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                dismissLoadingDialog();
            }
        }


    }

    @OnClick(R.id.ll_type)
    void typeClick() {
        showTypePopupWindow();
    }

    @OnClick(R.id.iv_video)
    void addVideoClick() {
        if (mImageList.size() > 1) {
            ToastUtil.showMessage(mContext, "不能同时上传图片和视频");
            return;
        }
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (TextUtils.isEmpty(mVideoPath)) {
                    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, REQUEST_VIDEO);
                } else {
                    ArrayList<ImageItem> list = new ArrayList<>();
                    ImageItem imageItem = new ImageItem();
                    File file = new File(mVideoPath);
                    if (file.exists()) {
                        imageItem.path = mVideoPath;
                    } else {
                        imageItem.url = Url.FILE_DOWNLOAD + mVideoPath;
                    }
                    imageItem.isVideo = true;
                    list.add(imageItem);
                    Intent intentPreview = new Intent(mContext, ImageAndVideoPreviewSeeActivity.class);
                    intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, 0);
                    intentPreview.putParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS, list);
                    startActivity(intentPreview);
                }
            }
        }, R.string.ask_again, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    @OnLongClick(R.id.iv_video)
    boolean addVideoLongClick() {
        if (mIvVideoDelete.isShown()) {
            mIvVideoDelete.setVisibility(View.GONE);
        } else {
            mIvVideoDelete.setVisibility(View.VISIBLE);
        }
        return true;
    }

    @OnClick(R.id.iv_video_delete)
    void addVideoDeleteClick() {
        mIvVideoDelete.setVisibility(View.GONE);
        mIvVideo.setImageResource(R.drawable.icon_add_res);
        mVideoPath = null;
        mIvPlay.setVisibility(View.GONE);
    }

    public String getPath(Uri uri) {
        String[] projection = {MediaStore.Video.Media.DATA};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

    private void initImageRv() {
        mImageItem.path = "";
        mImageItem.addTime = System.currentTimeMillis();
        if (mImageList.size() < 3) {
            mImageList.add(mImageItem); // 插入 + 号占位数据
        }
        mCustomImageAdapter = new CommonAdapter<ImageItem>(mContext, mImageList, R.layout.item_grid_image_view) {
            @Override
            protected void convert(final ViewHolder holder, final ImageItem imageItem, final int position) {
                ImageView ivItem = (ImageView) holder.getView(R.id.iv_item);
                holder.getView(R.id.iv_delete).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showDeleteConfirmDialog(position);
                    }
                });

                if (TextUtils.isEmpty(imageItem.path) && TextUtils.isEmpty(imageItem.url)) {
                    holder.getView(R.id.iv_delete).setVisibility(View.GONE);
                    ivItem.setImageResource(R.drawable.icon_add_res);
                    ivItem.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (!TextUtils.isEmpty(mVideoPath)) {
                                ToastUtil.showMessage(mContext, "不能同时上传图片和视频");
                                return;
                            }
                            showPopupWindow();
                        }
                    });
                } else {
                    if (TextUtils.isEmpty(imageItem.path)) {
                        ImageLoader.display(mContext, Url.FILE_DOWNLOAD + imageItem.url, ivItem);
                    }
                    if (TextUtils.isEmpty(imageItem.url)) {
                        ImageLoader.display(mContext, new File(imageItem.path), ivItem);
                    }

                    ivItem.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            if (mShowDelete) {
                                mShowDelete = false;
                            } else {
                                mShowDelete = true;
                            }
                            notifyDataSetChanged();
                            return false;
                        }
                    });

                    ivItem.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (TextUtils.isEmpty(mImageList.get(position).path) && TextUtils.isEmpty(mImageList.get(position).url)) {
                                return;
                            }

                            if (mImageList.contains(mImageItem)) {
                                mImageList.remove(mImageItem);
                            }

                            Intent intentPreview = new Intent(UploadMaterialActivity.this, ImageAndVideoPreviewSeeActivity.class);
                            intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                            intentPreview.putParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS, mImageList);
                            startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
                        }
                    });

                    if (mShowDelete) {
                        holder.getView(R.id.iv_delete).setVisibility(View.VISIBLE);
                    } else {
                        holder.getView(R.id.iv_delete).setVisibility(View.GONE);
                    }
                }

            }
        };


        mRvUserUpload.setLayoutManager(new GridLayoutManager(mContext, 3, LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mRvUserUpload.setAdapter(mCustomImageAdapter);
    }

    private void showTypePopupWindow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_type_list, null);
        // 强制隐藏键盘
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        if (mTypePopupWindow == null) {
            mTypePopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            mTypePopupWindow.setFocusable(true);
            mTypePopupWindow.setOutsideTouchable(true);
            /* mTypePopupWindow.setBackgroundDrawable(new BitmapDrawable());*/
            mTypePopupWindow.setAnimationStyle(R.style.AnimationBottomDialog);
            mTypePopupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        mTypePopupWindow.dismiss();
                        return true;
                    }
                    return false;
                }
            });
            mTypePopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    WindowUtil.setPopWindowBackgroundAlpha(UploadMaterialActivity.this, 1f);
                }
            });
            view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mTypePopupWindow.dismiss();
                }
            });

            final List<String> typeList = new ArrayList<>();
            final List<String> typeIdList = new ArrayList<>();

            for (String key : mTypeLinkedTreeMap.keySet()) {
                String value = mTypeLinkedTreeMap.get(key);
                typeIdList.add(key);
                typeList.add(value);
            }

            RecyclerView rv = view.findViewById(R.id.rv_type);
            CommonAdapter<String> adapter = new CommonAdapter<String>(mContext, typeList, R.layout.item_resource_type) {
                @Override
                protected void convert(ViewHolder holder, String s, int position) {
                    holder.setText(R.id.tv_type, s);
                }
            };
            adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                    mTypePopupWindow.dismiss();
                    mTypeId = typeIdList.get(position);
                    mTvType.setText(typeList.get(position));
                }

                @Override
                public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                    return false;
                }
            });
            int height = DisplayUtil.getInstance(mContext).dp2px(1);
            int color = Color.parseColor("#F4F4F4");
            RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
            rv.addItemDecoration(decoration);
            rv.setLayoutManager(new LinearLayoutManager(this));
            rv.setAdapter(adapter);

        }
        WindowUtil.setPopWindowBackgroundAlpha(UploadMaterialActivity.this, 0.7f);
        mTypePopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    private void showPopupWindow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_album_camera, null);
        // 强制隐藏键盘
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        if (mPopupWindow == null) {

            ImagePicker imagePicker = ImagePicker.getInstance();
            imagePicker.setImageLoader(new GlideImageLoader());
            imagePicker.setCrop(false);
            imagePicker.setMultiMode(true);
            imagePicker.setShowCamera(true);

            mPopupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            mPopupWindow.setFocusable(true);
            mPopupWindow.setOutsideTouchable(true);
            /* mPopupWindow.setBackgroundDrawable(new BitmapDrawable());*/
            mPopupWindow.setAnimationStyle(R.style.AnimationBottomDialog);
            mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    if (motionEvent.getAction() == MotionEvent.ACTION_OUTSIDE) {
                        mPopupWindow.dismiss();
                        return true;
                    }
                    return false;
                }
            });
            mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    WindowUtil.setPopWindowBackgroundAlpha(UploadMaterialActivity.this, 1f);
                }
            });
            view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPopupWindow.dismiss();
                }
            });
            view.findViewById(R.id.btn_camera).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPopupWindow.dismiss();
                    checkPermission(new CheckPermListener() {
                        @Override
                        public void superPermission() {
                            Intent intent = new Intent(mContext, ImageGridActivity.class);
                            intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true);
                            intent.putExtra(ImageGridActivity.EXTRAS_IMAGES, (Serializable) mImageList);
                            startActivityForResult(intent, REQUEST_CAMERA);
                            mPopupWindow.dismiss();
                        }
                    }, R.string.ask_again, Manifest.permission.CAMERA);
                }
            });
            view.findViewById(R.id.btn_album).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mPopupWindow.dismiss();
                    checkPermission(new CheckPermListener() {
                        @Override
                        public void superPermission() {
                            ImagePicker.getInstance().setSelectLimit(10 - mImageList.size());
                            Intent intent = new Intent(mContext, ImageGridActivity.class);
                            intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, false);
                            startActivityForResult(intent, REQUEST_ALBUM);
                            mPopupWindow.dismiss();
                        }
                    }, R.string.ask_again, Manifest.permission.READ_EXTERNAL_STORAGE);
                }
            });
        }
        WindowUtil.setPopWindowBackgroundAlpha(UploadMaterialActivity.this, 0.7f);
        mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }


    private void showDeleteConfirmDialog(final int position) {
        ConfirmDialog mConfirmDialog = new ConfirmDialog(mContext, new ConfirmDialog.OnBottomButtonClickListener() {
            @Override
            public void onLeftButtonClick(View view) {
            }

            @Override
            public void onRightButtonClick(View view) {
                mImageList.remove(position);
                if (!mImageList.contains(mImageItem)) {
                    mImageList.add(mImageItem);
                }
                mCustomImageAdapter.notifyDataSetChanged();
            }

        });
        mConfirmDialog.showDialog("确认删除？", "取消", "确认");
    }

    private void httpUploadResource(String content, String imageId, String videoId, final String typeId) {
        if (mResourceBean != null) {
            httpUpdateResource(mResourceBean.getId(), content, imageId, typeId, videoId);
            return;
        }

        try {
            ResourceBizFactory.createBiz().addResource(this, content, imageId, typeId, videoId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    EventBus.getDefault().post(new EventMessage(EventTag.MATERIAL_REFRESH_ME, typeId));
                    finish();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            dismissLoadingDialog();

        }
    }

    private void httpUpdateResource(String resId, String content, String imageId, String videoId, final String typeId) {
        try {
            ResourceBizFactory.createBiz().updateResource(this, resId, content, imageId, videoId, typeId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    EventBus.getDefault().post(new EventMessage(EventTag.MATERIAL_REFRESH_ME, typeId));
                    finish();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            dismissLoadingDialog();

        }
    }

}
